import Wx, {wx} from "./Wx";

export default class GameClub {

    private button: any = null;
    private isShowing: boolean = false;

    private readonly style: { left: number, top: number, width: number, height: number; } = null;

    public constructor(style?: { left: number, top: number, width: number, height: number }) {
        this.style = style || {left: 0, top: 0, width: 10, height: 10};
    }

    public async show() {
        if (this.isShowing) { return; }
        this.isShowing = true;
        if (!this.button) { await this.createGameClubButton(); }
        if (this.button) {
            this.button.show();
        }
    }

    public hide() {
        if (!this.isShowing) { return; }
        this.isShowing = false;
        if (this.button) {
            this.button.hide();
        }
    }

    private async createGameClubButton() {
        if (Wx.isWeChatEnv()) {
            const sysInfo = wx.getSystemInfoSync();
            const style = this.style;
            style.left = (style.left || 0) * sysInfo.screenWidth / cc.winSize.width;
            style.top = (style.top || 0) * sysInfo.screenHeight / cc.winSize.height;
            style.width = (style.width || 40) * sysInfo.screenWidth / cc.winSize.width;
            style.height = (style.height || 40) * sysInfo.screenHeight / cc.winSize.height;

            this.button = wx.createGameClubButton({
                type: "image",
                icon:"green",
                style,
            });
            this.button.onTap((res) => {
                cc.log(res);
            });
        }
    }
}
